/**
 * Project Name:dsrs-systemservice-web
 * File Name:UserSecurityController.java
 * Package Name:com.bwda.dsrs.systemservice.controller;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.usersecurity.UserSecurityCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.usersecurity.UserSecurityCreateForm;
import com.bwda.dsrs.systemservice.domain.form.usersecurity.UserSecurityQueryForm;
import com.bwda.dsrs.systemservice.domain.form.usersecurity.UserSecurityUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.usersecurity.UserSecurityPo;
import com.bwda.dsrs.systemservice.domain.vo.usersecurity.UserSecurityVo;
import com.bwda.dsrs.systemservice.service.UserSecurityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * 用户安全策略
 *
 * @author liangjianyong
 * @date 2019/4/11
 */
@RestController
@RequestMapping("/s/usersecurity")
@Api(value = "/s/usersecurity", tags = "用户安全策略")
public class UserSecurityController extends BaseController {

    @Autowired
    private UserSecurityService userSecurityService;

    @ApiOperation(value = "查询安全策略", notes = "查询安全策略", httpMethod = "GET")
    @Loggable(descp = "查询安全策略",type = LogTypeEnum.QUERY,include = "")
    @PostMapping(value = "/queryUserSecurity")
    public ResponseEntity<UserSecurityVo> queryUserSecurity(@ApiParam(value = "用户编码", required = true) @RequestBody String userCode) throws SystemServiceException {
        //只能查看自己的安全策略
        if(!getUserCode().equals(userCode)){
            throw new SystemServiceException("只能查看自己的安全策略");
        }

        UserSecurityPo po = userSecurityService.queryWithValid(userCode);
        UserSecurityVo vo = CopyUtil.transfer(po, UserSecurityVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询用户安全策略", notes = "查询用户安全策略", httpMethod = "GET")
    @Loggable(descp = "查询用户安全策略",type = LogTypeEnum.QUERY,include = "")
    @PostMapping(value = "/querySecurityByUserCode")
    public ResponseEntity<UserSecurityPo> querySecurityByUserCode(@ApiParam(value = "用户编码", required = true) @RequestBody String userCode) throws SystemServiceException {
        //只能查看自己的安全策略
        if(!getUserCode().equals(userCode)){
            throw new SystemServiceException("只能查看自己的安全策略");
        }
        UserSecurityPo po = userSecurityService.querySecurityByUserCode(userCode);
        return getSuccessResult(po);
    }

    @ApiOperation(value = "查询用户安全策略数量", notes = "查询用户安全策略数量", httpMethod = "GET")
    @Loggable(descp = "查询用户安全策略数量",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid UserSecurityQueryForm form) throws SystemServiceException {
        UserSecurityCondition condition = this.getConditionByQueryForm(form);
        int count = userSecurityService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询用户安全策略列表", notes = "查询用户安全策略列表", httpMethod = "GET")
    @Loggable(descp = "查询用户安全策略列表",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<UserSecurityVo>> queryList(@ModelAttribute @Valid UserSecurityQueryForm form) throws SystemServiceException {
        UserSecurityCondition condition = this.getConditionByQueryForm(form);
        List<UserSecurityPo> poList = userSecurityService.queryList(condition);
        List<UserSecurityVo> voList = CopyUtil.transfer(poList, UserSecurityVo.class);
        return getSuccessResult(getListResponse(voList));
    }

    @ApiOperation(value = "查询用户安全策略列表(带分页)", notes = "查询用户安全策略列表(带分页)", httpMethod = "GET")
    @Loggable(descp = "查询用户安全策略列表(带分页)",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<UserSecurityVo>> queryPageList(@ModelAttribute @Valid UserSecurityQueryForm form) throws SystemServiceException {
        UserSecurityCondition condition = this.getConditionByQueryForm(form);
        List<UserSecurityVo> voList = new ArrayList<>();
        int count = userSecurityService.queryCount(condition);
        if (count > 0) {
            List<UserSecurityPo> poList = userSecurityService.queryList(condition);
            voList = CopyUtil.transfer(poList, UserSecurityVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "新增用户安全策略", notes = "新增用户安全策略", httpMethod = "POST")
    @Loggable(descp = "新增用户安全策略",type = LogTypeEnum.CREATE,include = "")
    @PostMapping(value = "/addUserSecurity")
    public ResponseEntity<UserSecurityVo> add(@RequestBody UserSecurityCreateForm form) throws SystemServiceException {
        UserSecurityPo po = CopyUtil.transfer(form, UserSecurityPo.class);
        userSecurityService.insert(po);
        UserSecurityVo vo = CopyUtil.transfer(po, UserSecurityVo.class);
        return getSuccessResult(vo);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改用户安全策略", notes = "修改用户安全策略", httpMethod = "POST")
    @Loggable(descp = "修改用户安全策略",type = LogTypeEnum.UPDATE,include = "")
    @PostMapping(value = "/updateUserSecurity")
    public ResponseEntity update(@RequestBody UserSecurityUpdateForm form) throws SystemServiceException {
        UserSecurityPo po = CopyUtil.transfer(form, UserSecurityPo.class);
        userSecurityService.update(po);
        return getSuccessResult();
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "删除用户安全策略", notes = "删除用户安全策略", httpMethod = "POST")
    @Loggable(descp = "删除用户安全策略",type = LogTypeEnum.DELETE,include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "用户编码", required = true) @RequestParam String userCode) throws SystemServiceException {
        userSecurityService.delete(userCode);
        return getSuccessResult();
    }

    /**
     * UserSecurityQueryForm转换为UserSecurityCondition
     *
     * @param form
     * @return
     */
    private UserSecurityCondition getConditionByQueryForm(UserSecurityQueryForm form) {
        UserSecurityCondition condition = CopyUtil.transfer(form, UserSecurityCondition.class);
        return condition;
    }

}